<?php
namespace app\common\service\weixin;

use app\common\service\UrlService;
use app\models\member\Member;
use app\models\member\OauthMemberBind;
use app\models\pay\PayOrder;
use app\modules\m\controllers\common\BaseController;

class TemplateService extends BaseController
{
    public static function PayNotice( $pay_order_id ){
        $pay_order_info = PayOrder::findOne( ['id'=>$pay_order_id] );
        if( !$pay_order_info ){
            return false;
        }

        $config = \Yii::$app->params['weixin'];
        RequestService::setConfig( $config['appid'],$config['token'],$config['appsecret'] );
        $openid = self::getOpenId($pay_order_info['member_id']);
        if( !$openid ){
            return false;
        }
        $template_id = "VQRD0Mp9bRbwdvmv4nSNwv6mOmx8MViGmAEe-oOW8d0";
        $data = [
            'first'=> [
                'value' =>"您的订单已支付成功~~" ,
                'color' =>'#173177'
            ],
            'keyword1'=> [
                'value' =>date("Ymd",strtotime( $pay_order_info['created_time'] )).$pay_order_info['id'],
                'color' =>'#173177'
            ],
            'keyword2'=> [
                'value' =>date("Y-m-d H:i",strtotime( $pay_order_info['pay_time'] )),
                'color' =>'#173177'
            ],
            'keyword3'=> [
                'value' =>$pay_order_info['pay_price'],
                'color' =>'#173177'
            ],
            'remark'=> [
                'value' =>'点击查看详情',
                'color' =>'#173177'
            ],
        ];
        return self::send( $openid,$template_id,UrlService::buildMUrl( "/usr/index" ),$data );

    }

    //发送模板
    public static function send( $openid,$template_id,$url,$data )
    {
        $msg = [
            'touser' => $openid,
            'template_id'=>$template_id,
            'url' => $url,
            'data' => $data
        ];
        $token = RequestService::getAccessToken();
        return RequestService::send("message/template/send?access_token=".$token,json_encode($msg),'POST');
        
    }

    public static function getOpenId( $member_id )
    {
        $open_infos = OauthMemberBind::findAll( ['member_id' => $member_id,'type' =>1 ]);

        if( !$open_infos ){
            return false;
        }

        foreach( $open_infos as $open_info ){
            if( self::getPublicByOpenId( $open_info['openid'] ) ){
                return $open_info['openid'];
            }
            return false;
        }
    }

    protected static function getPublicByOpenId( $openid )
    {
        $token = RequestService::getAccessToken();
        $info = RequestService::send("user/info?access_token={$token}&openid={$openid}&lang=zh_CN");
        if( !$info || isset( $info['errcode'] ) ){
            return false;
        }
        return true;
    }

    /**
     * 绑定微信通知提醒
     * @param $member_id
     */
    public static function bindNotice( $member_id ){
        $member_info = Member::findOne( ['id'=>$member_id] );
        if( !$member_info ){
            return false;
        }

        $config = \Yii::$app->params['weixin'];
        RequestService::setConfig( $config['appid'],$config['token'],$config['appsecret'] );
       $open_id = self::getOpenId( $member_id );
       if( !$open_id ){
            return false;
       }

       $template_id = "ywuQ7CgzvDURT4vOLZ8NYVNR4i1Ow5b7nWpKmUFfVQI";
        $data = [
            "first" => [
                "value" => "您好，您已注册并成功绑定微信",
                "color" => "#173177"
            ],
            "keyword1" =>[
                "value" => $member_info['mobile'],
                "color" => "#173177"
            ],
            "keyword2" =>[
                "value" => date("Y-m-d H:i",strtotime( $member_info['created_time'] ) ),
                "color" => "#173177"
            ],
            "remark" => [
                "value" => "感谢您支持".Yii::$app->params['title'],
                "color" => "#173177"
            ]
        ];

        return self::send( $open_id,$template_id,UrlService::buildMUrl("/user/index"),$data );
    }

}